﻿using System;
using System.Linq;
using DtoModels;
using Entities;
using Microsoft.AspNetCore.Mvc;
using Portal.Models;
using SqlSugar;

namespace Portal.Controllers
{
    public class GdMapController : Controller
    {
        private readonly SqlSugarClient _sqlSugarClient;

        public GdMapController(SqlSugarClient sqlSugarClient)
        {
            _sqlSugarClient = sqlSugarClient;
        }

        /// <summary>
        /// 获取地址
        /// </summary>
        /// <returns></returns> 
        public IActionResult GetRegion(RequestDistrictReqDto req)
        {
            var result = new DistrictResDto
            {
                Province = new ResultRegion {Name = req.province,},
                City = new ResultRegion {Name = req.city,},
                Area = new ResultRegion {AdCode = req.adCode, Name = req.area,}
            };


            var districtList = this._sqlSugarClient
                .Queryable<sys_District>()
                .Where(x => x.AD_Code == req.adCode)
                .ToList();

            if (districtList.Count <= 0)
            {
                throw new Exception("查找不到相关位置信息");
            }

            var district = districtList[0];

            if (string.IsNullOrWhiteSpace(req.area) == false)
            {
                var districtNameList = districtList.Where(m => m.AD_Name == req.area).ToList();
                if (districtNameList.Count > 0)
                {
                    district = districtNameList.FirstOrDefault();
                }
            }
            if (district != null)
            {
                result = GetDistrict(result, district.Id, 4);
                return Json(new
                {
                    code = GlobalVariable.Ok,
                    Data = result
                });
            }

            return Json(new
            {
                code = GlobalVariable.Error,
                Data = result
            });

        }

        private DistrictResDto GetDistrict( DistrictResDto resuelt, string id, int leveId)
        {
            var district = this._sqlSugarClient.Queryable<sys_District>().First(m => m.Id == id);
            if (district != null)
            {
                switch (leveId)
                {
                    case 4:
                        resuelt.Area.Id = district.Id;
                        resuelt.Area.Name = district.AD_Name;
                        resuelt.Area.AdCode = district.AD_Code;
                        GetDistrict( resuelt, district.ParentId, leveId - 1);
                        break;
                    case 3:
                        resuelt.City.Id = district.Id;
                        resuelt.City.Name = district.AD_Name;
                        resuelt.City.AdCode = district.AD_Code;
                        GetDistrict(resuelt, district.ParentId, leveId - 1);
                        break;
                    case 2:
                        resuelt.Province.Id = district.Id;
                        resuelt.Province.Name = district.AD_Name;
                        resuelt.Province.AdCode = district.AD_Code;
                        break;
                    default: break;
                }
            }
            return resuelt;
        }


    }
}